package com.woniu.inlet.web;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.woniu.command.Command;
import com.woniu.command.workingProcedureTypeCommand.AddWorkingProcedureTypeCommand;
import com.woniu.command.workingProcedureTypeCommand.DeleteWorkingProcedureTypeCommand;
import com.woniu.command.workingProcedureTypeCommand.UpdateWorkingProcedureTypeCommand;
import com.woniu.inlet.vo.WorkingProcedureTypeVo;
import com.woniu.outlet.po.WorkingProcedureTypePo;
import com.woniu.service.WorkingProcedureTypeService;
import com.woniu.utils.ResponseResult;
import com.woniu.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Api(tags = "工序分类管理")
@Slf4j
@RequestMapping("/workingProcedureType/")
public class WorkingProcedureTypeController {

    @Autowired
    private WorkingProcedureTypeService workingProcedureTypeService;

    @Autowired
    private SystemLogController systemLogController;

    @GetMapping("/workingProcedureTypes")
    @ApiOperation("查询工序分类")
    public ResponseResult<List<WorkingProcedureTypeVo>> findworkingProcedureTypes() throws Exception{
        List<WorkingProcedureTypePo>  workingProcedureTypePos =  workingProcedureTypeService.findWorkingProcedureTypes();
        List<WorkingProcedureTypeVo> workingProcedureTypeVos = new ArrayList<>();
        //对象的深拷贝
        for (WorkingProcedureTypePo item : workingProcedureTypePos){
            WorkingProcedureTypeVo workingProcedureTypeVo = new WorkingProcedureTypeVo();
            BeanUtils.copyProperties(item,workingProcedureTypeVo);
            workingProcedureTypeVos.add(workingProcedureTypeVo);
        }
        systemLogController.addSystemLog("查询工序分类");
        return new ResponseResult<>(workingProcedureTypeVos, "查询成功", ResultCode.SUCCESS.getCode());
    }

    @GetMapping("/workingProcedureTypes/id")
    @ApiOperation("根据id查询工序分类")
    public ResponseResult<WorkingProcedureTypeVo> getWorkingProcedureTypeById(Long id) throws Exception {
        WorkingProcedureTypePo workingProcedureTypePo = workingProcedureTypeService.getWorkingProcedureTypeById(id);

        //对象的深拷贝

        WorkingProcedureTypeVo workingProcedureTypeVo = new WorkingProcedureTypeVo();
        BeanUtils.copyProperties(workingProcedureTypePo, workingProcedureTypeVo);

        systemLogController.addSystemLog("根据id查询工序分类");
        return new ResponseResult<>(workingProcedureTypeVo, "查询成功", ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/add")
    @ApiOperation("添加工序分类")
    @ApiOperationSupport(ignoreParameters = {"id","spare1","spare2","spare3"})
    public ResponseResult<String> addWorkingProcedureType(WorkingProcedureTypePo workingProcedureTypePo){
        Command command = new AddWorkingProcedureTypeCommand();
        command.execute(workingProcedureTypePo);

        systemLogController.addSystemLog("添加工序分类");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"添加成功",ResultCode.SUCCESS.getCode());
    }

    @PostMapping("/update")
    @ApiOperation("修改工序分类")
    @ApiOperationSupport(ignoreParameters = {"spare1","spare2","spare3"})
    public ResponseResult<String> updateWorkingProcedureType(WorkingProcedureTypePo workingProcedureTypePo){
        Command command = new UpdateWorkingProcedureTypeCommand();
        command.execute(workingProcedureTypePo);

        systemLogController.addSystemLog("修改工序分类");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"修改成功",ResultCode.SUCCESS.getCode());
    }


    @PostMapping("/delete")
    @ApiOperation("根据id删除工序分类")
    public ResponseResult<String> deleteWorkingProcedureType(Long id){
        Command command = new DeleteWorkingProcedureTypeCommand();
        command.execute(id);
        systemLogController.addSystemLog("根据id删除工序分类");
        return new ResponseResult<>(ResultCode.SUCCESS.getMessage(),"删除成功",ResultCode.SUCCESS.getCode());
    }


}
